<?php
# Part of RASS unittest fixtures: dir mgmt.
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

class RassDirMgmtTest extends UnitTestCase {
    function TestRassClientlib() {
        $this->UnitTestCase("Rass Dir Mgmt Tests");
    }

    function testGetRootFeed() {
      try {
        # check the rootdir
        $rass = new Rass(USER, PWD, BASE);
        $rass->setSSL(SSL);
        $feed = $rass->getDirFeed();
        foreach ($feed as $entry) {
          $this->assertEqual($entry->path, "/");
          $this->assertEqual($entry->kind, "root");
          $this->assertEqual($entry->content->params->name->text, "");
          break;
        }
      }
      catch(Zend_Gdata_App_Exception $e) {
        $this->assertEqual("ok", $e->getResponse()->getBody());
      }
    }

    function testCreateSubdir() {
      try {
        # create a subdir
        $rass = new Rass(USER, PWD, BASE);
        $rass->setSSL(SSL);
        $entry = $rass->createDir("chiquita");
        $this->assertEqual($rass->getResponse()->getHeader("Location"), BASE . "dir/chiquita/");
        
        # retrieve the subdir
        $feed = $rass->getDirFeed("chiquita");
        foreach ($feed as $entry) {
          $this->assertEqual($entry->path, "/chiquita");
          $this->assertEqual($entry->kind, "root");
          $this->assertEqual($entry->content->params->name->text, "chiquita");
          break;
        }
      }
      catch(Zend_Gdata_App_Exception $e) {
        $this->assertEqual("ok", $e->getResponse()->getBody());
      }
    } 
    
    function testCreateSubSubdir() {
      try {
        $rass = new Rass(USER, PWD, BASE);
        $rass->setSSL(SSL);
        # create a subdir of the subdir
        $entry = $rass->createDir("chiquita/banana");
        $this->assertEqual($rass->getResponse()->getHeader("Location"), BASE . "dir/chiquita/banana/");
      }
      catch(Zend_Gdata_App_Exception $e) {
        $this->assertEqual("ok", $e->getResponse()->getBody());
      }
    }
    
    function testDeleteNotEmptyDir() {
      // DELETE a dir that doesn't exist
      $status = 0;
      try {
        $rass = new Rass(USER, PWD, BASE);
        $rass->setSSL(SSL);
        # create the dir that has an existing subdir
        $entry = $rass->deleteDir("chiquita");
      }
      catch(Zend_Gdata_App_Exception $e) {
        $status = $e->getResponse()->getStatus();
      }
      $this->assertEqual($status, 403); 
    }
    
    function testCreateExistingDir() {
      $status = 0;
      try {
        $rass = new Rass(USER, PWD, BASE);
        $rass->setSSL(SSL);
        # create the subdir twice
        $entry = $rass->createDir("chiquita");
      }
      catch(Zend_Gdata_App_Exception $e) {
        $status = $e->getResponse()->getStatus();
      }
      $this->assertEqual($status, 409); 
    }
    
    function testCreateMultipleHierarchies() {
      $status = 0;
      try {
        $rass = new Rass(USER, PWD, BASE);
        $rass->setSSL(SSL);
        $entry = $rass->createDir("chiquita/zozo/banana");
        $this->assertEqual($rass->getResponse()->getHeader("Location"), BASE . "dir/chiquita/zozo/banana/");
      }
      catch(Zend_Gdata_App_Exception $e) {
        $this->assertEqual("ok", $e->getResponse()->getBody());
      }
    }
      
    function testDeleteNonDir() {
      // DELETE a dir that doesn't exist
      $status = 0;
      try {
        $rass = new Rass(USER, PWD, BASE);
        $rass->setSSL(SSL);
        $rass->deleteDir("no-banana");
        assert(False);
      }
      catch(Zend_Gdata_App_Exception $e) {
        $status = $e->getResponse()->getStatus();
      }
      $this->assertEqual($status, 404); 
    }
      
        
    function testDeleteSubdirs() 
    {
      try {
        # delete the subsubdir first 
        $rass = new Rass(USER, PWD, BASE);
        $rass->setSSL(SSL);
        $rass->deleteDir("chiquita/banana");
        $this->assertEqual($rass->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        $this->assertEqual("ok1", $e->getResponse()->getBody());
      }
      try {
        # delete the subdir
        $rass = new Rass(USER, PWD, BASE);
        $rass->setSSL(SSL);
        $rass->deleteDir("chiquita", True);
        $this->assertEqual($rass->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        $this->assertEqual("ok2", $e->getResponse()->getBody());
      }
    }
    
}
